import jade.content.Concept;
import jade.content.ContentElement;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

public class Agent2 extends Agent implements AgentVocabulary{
//Vanzatorul = cel care detine lista de cumparaturi
	private String[] list;
	private int[] preturi;
	private boolean parfumTrimis=false;
	private boolean allDone=false;
	private SLCodec sl=new SLCodec();
	private MessageOntology ont=(MessageOntology) MessageOntology.getInstance();
	protected void setup(){
		String[] tmplist={"Produs1","Produs2","Produs3"};
		this.list=tmplist;
		int[] tmppret={10,15,20};
		this.preturi=tmppret;
		getContentManager().registerLanguage(sl);
		getContentManager().registerOntology(ont);
		addBehaviour(new CyclicBehaviour() {			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			@Override
			public void action() {
				// TODO Auto-generated method stub	
				int i;
				allDone=true;
				for (i=0;i<3;i++){
					if (list[i]!="#") allDone=false;
				}
				//if (allDone) doDelete();
				ACLMessage msg = receive();
				if (msg == null) { block(); return; }
				//System.out.println(msg.toString());				
				ACLMessage snd;
				int item;
				int pret;
				try {
					ContentElement cnt=getContentManager().extractContent(msg);
					MessageBody cp=(MessageBody)((Action)cnt).getAction();
					//System.out.println(cp.getAction());
					//System.out.println(cp.getContent());
					switch (cp.getAction()) {
					case "INIT":
						snd=msg.createReply();
						cp.setAction("INIT");
						cp.setContent("Init confirmed");
						snd.setLanguage(sl.getName());
						snd.setOntology(ont.getName());
						getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
						send(snd);
						break;
					case "PARFUM":
						parfumTrimis=true;
						snd=msg.createReply();
						cp.setAction("PARFUM");
						cp.setContent("Parfum confirmed");
						System.out.println("Parfum primit");
						snd.setLanguage(sl.getName());
						snd.setOntology(ont.getName());
						getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
						send(snd);
						break;
					case "CUMPARA":
						if (parfumTrimis==false){
							snd=msg.createReply();
							snd.setLanguage(sl.getName());
							snd.setOntology(ont.getName());
							cp.setAction("CUMPARA");
							cp.setContent("Error");							
							getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
							send(snd);
						}
						else{
							snd=msg.createReply();
							snd.setLanguage(sl.getName());
							snd.setOntology(ont.getName());
							if (allDone==true){
								cp.setAction("SIGKILL");
								cp.setContent("1");
							}
							else{
								item=Integer.parseInt(cp.getContent());
								cp.setAction("CUMPARA");
								if (list[item]=="#"){
									cp.setContent("Deja cumparat");									
								}
								else{
									System.out.println("Produsul nr "+item+" a fost de cumparat");
									list[item]="#";
									cp.setContent("Accepted");
								}
							}
							getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
							send(snd);
							if (allDone==true){
								System.out.println("Agent nr 2 oprit");
								doDelete();
							}
						}
						break;
					case "NEGOCIERE":
						if (parfumTrimis==false){
							snd=msg.createReply();
							snd.setLanguage(sl.getName());
							snd.setOntology(ont.getName());
							cp.setAction("NEGOCIERE");
							cp.setContent("Error");
							getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
							send(snd);
						}
						else{
							snd=msg.createReply();
							snd.setLanguage(sl.getName());
							snd.setOntology(ont.getName());
							cp.setAction("NEGOCIERE");
							item=Integer.parseInt(cp.getContent());
							pret=cp.getPret();
							if (preturi[item]<=pret){
								cp.setContent("Denied");
							}
							else{
								preturi[item]=preturi[item]-pret;
								System.out.println("Noul pret al produsului nr "+item+" este "+preturi[item]);
								cp.setContent("Accepted");
								cp.setPret(preturi[item]);
							}
							getContentManager().fillContent(snd, new Action(msg.getSender(),cp));
							send(snd);
						}
						break;
					}
				} catch (CodecException
						| OntologyException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
	}
}
